<?php

namespace app\asking\controller;

use think\Controller;

class Comment extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->BusinessModel = model('Business.User');
        $this->PostModel = model('Post.Post');
        $this->CollectModel = model('Post.Collect');
        $this->CommentModel = model('Post.Comment');
    }

    // 评论数据
    public function index(){
        if($this->request->isPost()){
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');
            $pid = $this->request->param('pid',0,'trim');

            // 查询帖子
            $post = $this->PostModel->find($postid);
            if(!$post){
                $this->error('帖子信息未知');
                exit;
            }

            $top = $this->CommentModel
                ->with(['business','post'])
                ->where(['postid'=>$postid,'pid'=>$pid])
                ->order(['pid asc','id asc'])
                ->select();
            
            if(empty($top)){
                $this->error('暂无评论');
                exit;
            }

            // 循环顶级评论
            foreach($top as $key=>&$item){
                $item['children'] = $this->CommentModel->sublist($item['id']);
            }

            $this->success('返回评论列表',null,$top);
        }
    }


    // 评论
    public function add(){
        if($this->request->isPost()){
            $pid = $this->request->param('pid',0,'trim');
            $content = $this->request->param('content','','trim');
            $postid = $this->request->param('postid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // var_dump($postid);
            // exit;
            // 查找帖子
            $post = $this->PostModel->find($postid);
            if(!$post){
                $this->error('帖子信息未知');
                exit;
            }

            // 查找用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 组装数据
            $data = [
                'pid' => $pid,
                'content' => $content,
                'busid' => $busid,
                'postid' => $postid,
                'status' => 0
            ];

            $result = $this->CommentModel->validate('common/Post/Comment')->save($data);
            if($result === FALSE){
                $this->error('发表评论失败');
                exit;
            }else{
                $this->success('发表评论成功');
                exit;
            }
        }
    }


    // 点赞
    public function like(){
        if($this->request->isPost()){
            $comid = $this->request->param('comid',0,'trim');
            $busid = $this->request->param('busid',0,'trim');
            $postid = $this->request->param('postid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询帖子
            $post = $this->PostModel->find($postid);
            if(!$postid){
                $this->error('帖子信息未知');
                exit;
            }

            // 查询评论
            $comment = $this->CommentModel->find($comid);
            if(!$comment){
                $this->error('评论信息未知');
                exit;
            }

            $data = [
                'id' => $comid
            ];

            // 判断是否点过赞
            $like_list = $comment['like_list'];

            // 判断元素是否在数组内
            if(in_array($busid,$like_list)){
                // 如果在说明点过赞，要取消点赞
                $index = array_search($busid,$like_list);
                unset($like_list[$index]);
                if(empty($like_list)){
                    $data['like'] = NULL;
                }else{
                    $data['like'] = implode(',',$like_list);
                }
    
                $result = $this->CommentModel->isUpdate(true)->save($data);
    
                if($result === FALSE){
                    $this->error('取消点赞失败');
                    exit;
                }else{
                    $this->success('取消点赞成功');
                    exit;
                }
            }else{
                // 不在则没点过赞，要点赞
                $like_list[] = $busid;

                if(empty($like_list)){
                    $data['like'] = NULL;
                }else{
                    $data['like'] = implode(',',$like_list);
                }
    
                $result = $this->CommentModel->isUpdate(true)->save($data);
    
                if($result === FALSE){
                    $this->error('点赞失败');
                    exit;
                }else{
                    $this->success('点赞成功');
                    exit;
                }
            }

            
        }
    }


    // 删除
    public function del(){
        if($this->request->isPost()){
            $comid = $this->request->param('comid',0,'trim');

            // 查询评论
            $comment = $this->CommentModel->find($comid);
            if(!$comment){
                $this->error('评论信息未知');
                exit;
            }

            $result = $this->CommentModel->where(['id'=>$comid])->delete();
            if($result === FALSE){
                $this->error('删除失败');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }


}
